//
//  ZEmoticonKeyBoardView.swift
//  swift-weibo
//
//  Created by zero on 17/3/30.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit

class ZEmoticonKeyBoardView: UIView {

    override init(frame: CGRect) {
        
        super.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth , height: 216))
        
        //  布局
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  布局
    
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        
        //  添加底部视图
        addSubview(emoticonBottomView)
        
        //  约束
        emoticonBottomView.snp.makeConstraints { (make) in
            
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        
        emoticonBottomView.scrolleTo = {[weak self] (tag)->() in
            
            let indexPath = IndexPath(item: 0, section: tag - 2000)
            
            self?.emotionCollectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
            
        }
        //  添加中间的 表情视图
        
        addSubview(emotionCollectionView)
        
        //  代理 
        emotionCollectionView.delegate = self
        
        //  约束
        
        emotionCollectionView.snp.makeConstraints { (make) in
            
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(emoticonBottomView.snp.top)
        }
        
        
        //  添加
        addSubview(pageControl)
        
        //  约束
        pageControl.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonBottomView.snp.top).offset(10)
            
        }
   
        self.layoutIfNeeded()
        
        let indexPath = IndexPath(item: 0, section: 1)
        
        DispatchQueue.main.async {
            
            self.emotionCollectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
            
        }
        
  
    }
    
    //  底部视图
    fileprivate var emoticonBottomView:ZEmoticonBottomView = ZEmoticonBottomView()
    
    //  中间的 表情视图
    fileprivate var emotionCollectionView:ZEmoticonCollectionView = ZEmoticonCollectionView()
    
    //  添加pageControl
    fileprivate var pageControl:UIPageControl = {
       
        let page = UIPageControl()
        
        page.isUserInteractionEnabled = false

        page.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        page.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        
        page.hidesForSinglePage = true
        
        return page
    }()

}

extension ZEmoticonKeyBoardView:UICollectionViewDelegate{
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let centerX = scrollView.contentOffset.x + ScreenWidth * 0.5
        
        let point = CGPoint(x: centerX, y: 0)
        
        //  判断
        if let indexPath = emotionCollectionView.indexPathForItem(at: point){
            
            emoticonBottomView.setButtonTag(buttonTag: indexPath.section)
            
            pageControl.currentPage = indexPath.item
            pageControl.numberOfPages = ((ZBundleTools.share.getThreeArray())[indexPath.section]).count
        }
        
        
    }
    
}
